<?
/*
**天时同城退票及验票通知
**2015-06-30
**by pzp
*/
if (!class_exists("go_sql")){
	include("/var/www/html/new/conf/le.je");
	$le=new go_sql();
	$le->connect();
}
include_once('/var/www/html/new/d/module/api/FuRongYuan/furongyuan.class.php');
include('/var/www/html/new/d/module/api/function.class.php');

$PFT_infunction = new AllFunction();
	
// $txt= json_encode($_REQUEST);
// logfile(date('Y-m-d H:i:s').'---'.$txt);
$txt = '{"_pid":"123280","method":"validate","format":"json","my_orders_id":"8037497","another_orders_id":"1551431","supplier_id":"120910","code":"9093192230","amount":"14","_sig":"C146C2AFAC353689DFC023D4E38BD413"}';

$arr = json_decode($txt,true);
if($arr['method'] == 'validate'){
	$another_orders_id = $arr['another_orders_id'];
}elseif($arr['method'] == 'refundResult'){
	$another_orders_id = $arr['orders_id'];
} 
//第三方平台订单ID
$pft_order = select_order($another_orders_id);
// print_r($pft_order );exit;
if(strlen($pft_order)>50){
	$pft_order_xml=simplexml_load_string($pft_order);
	$UUsalerid  = (string)$pft_order_xml->Rec->UUsalerid;
	$UUlid  = (string)$pft_order_xml->Rec->UUlid;
	$UUcode  = (string)$pft_order_xml->Rec->UUcode;
	$UUtnum  = (int)$pft_order_xml->Rec->UUtnum;
	$UUtprice  = (float)$pft_order_xml->Rec->UUtprice;

	// echo $terminal_type;exit;
	switch ($UUlid){
		case 9056:  //海源南娱
			$authcode = 'd1b7a0e111bd573fdfcb74537e4485b1';
		break;
		case 9213:  //百合 南娱
			$authcode = '352dfd64cb040ab0be38272f2f41d6db';
		break;
		case 9391:  //鸿利 南娱
			$authcode = '33c80d5424034fcd28d1226bf5e793dc';
		break;			
		case 9923:  //测试景区
			$authcode = '1afecdbdf5541a9780b3fe199e7da054';
		break;
		default:
		break;
	}
	$_sig = $arr['_sig'];
	$arr_sign = ksort_sign($arr,$authcode);
	if($_sig == $arr_sign){
		$method = $arr['method'];
		if($method == 'validate'){//验票通知
			$supplier_id = $arr['supplier_id']; //验证景区ID
			$amount = $arr['amount'];//验证数量
			$my_orders_id = $arr['my_orders_id'];//天时同城平台订单ID
			if($amount < $UUtnum){
				$totalprice = $UUtprice*$amount;
				$up_otum=$inSide->Order_Change_Pro($another_orders_id,$amount,$totalprice);
			}
			// echo $UUsalerid."->".$UUcode."->".$actiontime;exit;
			$terminal = $PFT_infunction->Compatible_Check_IN($UUsalerid,$UUcode,date('Y-m-d H:i:s'));
			if($terminal == 100){
				$message = '成功';
				$repMsg = 'true';
			}else{
				$message = $terminal;
				$repMsg = "false";
			}
			logfile($terminal);
			echo '{"success":"'.$repMsg.'","message":"'.$message.'"}';
		}	
		elseif($method == 'refundResult'){ //退款通知
			$type  = $arr['type']; // 3退票成功，4退票不通过
			$orders_id = $arr['orders_id']; //第三方平台订单ID
			if($type == 3){
				$str = "update uu_order_fx_details set memo='tstc_Refund_success' where orderid=".$orders_id." limit 1";
				$GLOBALS['le']->query($str);
				
			}else{
				$str = "update uu_order_fx_details set memo='tstc_Refund_fail' where orderid=".$orders_id." limit 1";
				$GLOBALS['le']->query($str);
			}
			$message = '成功';
			$repMsg = 'true';
			echo '{"success":"'.$repMsg.'","message":"'.$message.'"}';
		}
	}
	else{
		echo '{"success":"fail","message":"签名校验不通过"}';
	}	
}
else{
	echo '{"success":"fail","message":"此订单不存在"}';
}


//拼接数据及加密
function ksort_sign($array,$authcode){
	unset($array['_sig']);
	ksort($array);
	$o=""; 
	foreach ($array as $k=>$v){ 
		$o.= "$k=".$v."&"; 
	} 
	$post_data=substr($o,0,-1);	
	// echo $authcode."&".$post_data."&".$authcode;exit;
	$_sig = strtoupper(md5($authcode."&".$post_data."&".$authcode));
	return $_sig;
}

function select_order($pft_ordernum){
	global $inSide;
	$inSide = new SoapClient(null,array(
			"location" => "http://localhost/open/openService/pft_insideMX.php",
			"uri" => "www.16u.com?ac_16u=16ucom|pw_16u=c33367701511b4f6020ec61ded352059|auth_16u=true")
	);
	$pft_order = $inSide->Order_Globle_Search(
		0,0,0,0,"",
		"","","","","",
		"","",$pft_ordernum,"",0,
		"","","",1,0,
		0,1,"","","",
		"","","","","",
		"","","1","","");
	return $pft_order ;
}

function Err_Word($r){
	$errCode = array(
		'105'=>'找不到订单',
		'121'=>'订单状态出错,订单已验证',
		'119'=>'订单状态出错,已取消或其他状态',
		'521'=>'验证间隔时间未到',
		'133'=>'游玩时间未到',
		'134'=>'游玩时间已过',
		'118'=>'终端服务器无响应',
		'1070'=>'订单未支付',
	);
	$eword=$errCode[$r];
	if (!$eword) $eword='其他错误';
	return $eword;
}

function logfile($txt){
	$fp = fopen('/var/www/html/new/d/module/api/TianShiTongCheng/tstc_note.txt',"a");
	flock($fp, LOCK_EX) ;
	fwrite($fp,$txt."\n");
	flock($fp, LOCK_UN);
	fclose($fp); 
}

?>